﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System.Reflection;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Drawing;
using System.IO;
using System.Data.SqlTypes;
using System.Threading.Tasks;
using MySqlX.XDevAPI;
using System.Data.SqlClient;

namespace Meiam.System.Interfaces
{
    public class DB
    {
        public static string strmsg="";

        public static DataTable Query(string SQLString, string connectionString)
        {
            using (MySqlConnection MySqlConnection = new MySqlConnection(connectionString))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    MySqlConnection.Open();
                    MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(SQLString, MySqlConnection);
                    sqlDataAdapter.Fill(dataSet, "ds");
                }
                catch (MySqlException ex)
                {
                    strmsg= ex.ToString();
                }
                finally
                {
                    MySqlConnection.Close();
                }

                return dataSet.Tables[0];
            }
        }     
        public static DataTable OracleQuery(string SQLString, string connectionString)
        {
            using (OracleConnection OracleConnection = new OracleConnection(connectionString))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    OracleConnection.Open();
                    OracleDataAdapter sqlDataAdapter = new OracleDataAdapter(SQLString, OracleConnection);
                    sqlDataAdapter.Fill(dataSet, "ds");
                }
                catch (OracleException ex)
                {
                    strmsg = ex.ToString();
                }
                finally
                {
                    OracleConnection.Close();
                }

                return dataSet.Tables[0];
            }

        }

        public static DataTable MSSQuery(string SQLString, string connectionString)
        {
            using (SqlConnection SqlConnection = new SqlConnection(connectionString))
            {
                DataSet dataSet = new DataSet();
                try
                {
                    SqlConnection.Open();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(SQLString, SqlConnection);
                    sqlDataAdapter.Fill(dataSet, "ds");
                }
                catch (SqlException ex)
                {
                    strmsg = ex.ToString();
                }
                finally
                {
                    SqlConnection.Close();
                }

                return dataSet.Tables[0];
            }
        }







        public static bool ExecuteNoQueryTran(List<string> SQLStringList, string connectionString)
        {
            using (MySqlConnection sqlConnection = new MySqlConnection(connectionString))
            {
                sqlConnection.Open();
                MySqlCommand sqlCommand = new MySqlCommand();
                sqlCommand.Connection = sqlConnection;
                MySqlTransaction sqlTransaction2 = sqlCommand.Transaction = sqlConnection.BeginTransaction();
                int i = 0;
                try
                {
                    for (i = 0; i < SQLStringList.Count; i++)
                    {
                        string text = SQLStringList[i];
                        if (text.Trim().Length > 1)
                        {
                            sqlCommand.CommandText = text;
                            sqlCommand.ExecuteNonQuery();
                        }
                    }

                    sqlTransaction2.Commit();
                    sqlConnection.Close();
                    sqlCommand.Dispose();
                    return true;
                }
                catch (Exception ex)
                {
                    strmsg = ex.ToString();
                    sqlTransaction2.Rollback();
                    return false;
                }
            }
        }

        public static bool OracleExecuteNoQueryTran(List<string> SQLStringList, string connectionString)
        {
            using (OracleConnection sqlConnection = new OracleConnection(connectionString))
            {
                sqlConnection.Open();
                OracleCommand sqlCommand = new OracleCommand();
                sqlCommand.Connection = sqlConnection;
                OracleTransaction sqlTransaction2 = sqlCommand.Transaction = sqlConnection.BeginTransaction();
                int i = 0;
                try
                {
                    for (i = 0; i < SQLStringList.Count; i++)
                    {
                        string text = SQLStringList[i];
                        if (text.Trim().Length > 1)
                        {
                            sqlCommand.CommandText = text;
                            sqlCommand.ExecuteNonQuery();
                        }
                    }

                    sqlTransaction2.Commit();
                    sqlConnection.Close();
                    sqlCommand.Dispose();
                    return true;
                }
                catch (Exception ex)
                {
                    strmsg = ex.ToString();
                    sqlTransaction2.Rollback();
                    return false;
                }
            }
        }





        public static string httpPost(string url, string para, string Token, string IsToKen)
        {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
            if (IsToKen.Equals("是"))
            {
                wReq.Headers.Add("authToken", Token);
            }
            wReq.Method = "Post";
            wReq.ContentType = "application/json;charset=utf-8";
            byte[] data = Encoding.Default.GetBytes(para);
            //byte[] data = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(para);
            wReq.ContentLength = data.Length;
            Stream reqStream = wReq.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }

        public static string httpPost_OCR(string url, string para, string Token, string IsToKen)
        {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
            if (IsToKen.Equals("是"))
            {
                wReq.Headers.Add("appKey", Token);
            }
            wReq.Method = "Post";
            wReq.ContentType = "application/json;charset=utf-8";
            byte[] data = Encoding.Default.GetBytes(para);
            //byte[] data = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(para);
            wReq.ContentLength = data.Length;
            Stream reqStream = wReq.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }




        public static string httpPost_Erp(string url, string para, string Token, string IsToKen)
        {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
            if (IsToKen.Equals("是"))
            {
                wReq.Headers.Add("systoken", Token);
            }
            wReq.Method = "Post";
            wReq.ContentType = "application/json;charset=utf-8";
            byte[] data = Encoding.Default.GetBytes(para);
            //byte[] data = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(para);
            wReq.ContentLength = data.Length;
            Stream reqStream = wReq.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }






        ///拼接Get方法
        /// </summary>
        public static string httpGet(string url, string Token, string IsToKen)
        {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
            if (IsToKen.Equals("是"))
            {
                wReq.Headers.Add("SYSTOKEN", Token);
            }
            wReq.Method = "Get";
            //wReq.ContentType = "application/json;charset=utf-8";
            wReq.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)wReq.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }

        ///拼接Get方法
        /// </summary>
        public static string httpGet2(string url, string Token, string IsToKen)
        {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
            if (IsToKen.Equals("是"))
            {
                wReq.Headers.Add("SYSTOKEN", Token);
            }
            wReq.Method = "Get";
            wReq.ContentType = "application/json;charset=utf-8";
            //wReq.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)wReq.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }




        ///拼接POST方法
        /// </summary>
        public static string httpPost2(string url, string Token, string IsToKen)
        {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
            if (IsToKen.Equals("是"))
            {
                wReq.Headers.Add("SYSTOKEN", Token);
            }
            wReq.Method = "Post";
            //wReq.ContentType = "application/json;charset=utf-8";
            wReq.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)wReq.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }


        public static List<T> TableToListModel<T>(DataTable dt) where T : new()
        {
            // 定义集合    
            List<T> ts = new List<T>();

            // 获得此模型的类型   
            Type type = typeof(T);
            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                // 获得此模型的公共属性      
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    

                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;

                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }

        public static int ToInt(object mValue)
        {
            int result = 0;
            if (int.TryParse(mValue.ToString(), out result))
            {
                return result;
            }

            return 0;
        }

        public static double ToDouble(object mValue)
        {
            double result = 0.0;
            if (double.TryParse(mValue.ToString(), out result))
            {
                return result;
            }

            return 0.0;
        }

        public static decimal ToDecimal(object mValue)
        {
            decimal result = default(decimal);
            if (decimal.TryParse(mValue.ToString(), out result))
            {
                return result;
            }

            return 0m;
        }

        public static DateTime ToDateTime(object mValue)
        {
            DateTime result = DateTime.Now;
            if (DateTime.TryParse(mValue.ToString(), out result))
            {
                return result;
            }

            return DateTime.Now;
        }


    }


}